﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DAL;
using KPIS.GERP.GBMS.DALFactory;

namespace KPIS.GERP.GBMS.BLL
{
    public class SourceTypeBLL : ISourceTypeBLL
    {
        private static readonly ISourceTypeDAL dal = DataAccess.CreateSourceType();

        public SourceTypeBLL()
        {

        }

        public IList<TreeViewInfo> TreeView(IList<TreeViewInfo> tView, bool isRootNode)
        {
            List<TreeViewInfo> treeView = new List<TreeViewInfo>();

            return dal.TreeView(treeView);
        }
         

        public void Insert(string code, string name, string descr)
        {
            SourceTypeInfo srcTypeInfo = new SourceTypeInfo();

            srcTypeInfo.Code = code;
            srcTypeInfo.Name = name;
            srcTypeInfo.Description = descr;
            srcTypeInfo.User = new UserRecordInfo(1, null);

            int chkError = dal.Insert(srcTypeInfo);

            if (chkError == 0)
            {
                // Error
            }


        }

        public string InsertWithCheckDuplicate(string code, string name, string descr)
        {
            SourceTypeInfo srcTypeInfo = new SourceTypeInfo();

            srcTypeInfo.Code = code;
            srcTypeInfo.Name = name;
            srcTypeInfo.Description = descr;
            srcTypeInfo.User = new UserRecordInfo(1, null);

            string errorString = dal.DuplicateCheck(srcTypeInfo);
            if (String.IsNullOrEmpty(errorString))
            {
                int chkError = dal.Insert(srcTypeInfo);

                if (chkError == 0)
                {
                    // Error
                }

                return "";
            }
            else
            {
                return errorString;
            }

        }

        public void Update(int seq, string code, string name, string descr)
        {
            SourceTypeInfo srcTypeInfo = new SourceTypeInfo(seq, code, name, descr, SystemInfo._ActiveRecordStatus, new UserRecordInfo(null, 1));

            int chkError = dal.Update(srcTypeInfo);

            if (chkError == 0)
            {
                // Error
            }
        }
        public string UpdateWithCheckDuplicate(int seq, string code, string name, string descr)
        {
            SourceTypeInfo srcTypeInfo = new SourceTypeInfo(seq, code, name, descr, SystemInfo._ActiveRecordStatus, new UserRecordInfo(null, 1));

            string errorString = dal.DuplicateCheck(srcTypeInfo);
            if (String.IsNullOrEmpty(errorString))
            {
                int chkError = dal.Update(srcTypeInfo);

                if (chkError == 0)
                {
                    // Error
                }

                return "";
            }
            else
            {
                return errorString;
            }

        }

        public void Delete(int seq)
        {
            int chkError = dal.Delete(seq);
        }

        public IList<SourceTypeInfo> Search(int seq)
        {
            return Search(seq, "", "", "");
        }

        public IList<SourceTypeInfo> Search(int seq, string code, string name, string descr)
        {
            SourceTypeInfo srcTypeInfo = new SourceTypeInfo(seq, code, name, descr, SystemInfo._ActiveRecordStatus, null);

            return dal.Search(srcTypeInfo);
        }
    }
}
